﻿using BCCommon;
using BCEntity.OA.Notification.NotificationCategory;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;

namespace BCData.OA.Notification.NotificationCategory
{
    /// <summary>
    /// 通知分类
    /// </summary>
	public class NotificationCategoryData : INotificationCategoryData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM OA_NotificationCategory";
        private const string QUERY = @"SELECT NotificationCategoryId,NotificationCategoryCode,NotificationCategoryName,IsPredefined,CreateTime FROM OA_NotificationCategory";
        public NotificationCategoryData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        public async Task<bool> Exists(long notificationCategoryId)
        {
            string sql = string.Format(@"{0} WHERE NotificationCategoryId=?NotificationCategoryId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?NotificationCategoryId", notificationCategoryId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<bool> Exists(string NotificationCategoryCode)
        {
            string sql = string.Format(@"{0} WHERE NotificationCategoryCode=?NotificationCategoryCode", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?NotificationCategoryCode", NotificationCategoryCode, DbType.AnsiString, size: 50);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<NotificationCategoryEntity> Add(NotificationCategoryEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("OA_NotificationCategory", new string[] { "NotificationCategoryCode", "NotificationCategoryName", "IsPredefined" });
            sql = string.Format("{0} {1} WHERE NotificationCategoryId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?NotificationCategoryCode", entity.NotificationCategoryCode, DbType.AnsiString, size: 50);
            parameters.Add("?NotificationCategoryName", entity.NotificationCategoryName, DbType.AnsiString, size: 30);
            parameters.Add("?IsPredefined", false, DbType.Boolean);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<NotificationCategoryEntity>(sql, parameters, databaseContext.Transaction);

        }

        public async Task<NotificationCategoryEntity> Update(NotificationCategoryEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("OA_NotificationCategory", new string[] { "NotificationCategoryCode", "NotificationCategoryName" }, new string[] { "NotificationCategoryId" });
            sql = string.Format("{0} {1} WHERE NotificationCategoryId=?NotificationCategoryId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?NotificationCategoryId", entity.NotificationCategoryId, DbType.Int64);
            parameters.Add("?NotificationCategoryCode", entity.NotificationCategoryCode, DbType.AnsiString, size: 50);
            parameters.Add("?NotificationCategoryName", entity.NotificationCategoryName, DbType.AnsiString, size: 30);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<NotificationCategoryEntity>(sql, parameters,databaseContext.Transaction);
        }

        public async Task<bool> Delete(long notificationCategoryId)
        {
            string sql = @"DELETE FROM OA_NotificationCategory WHERE NotificationCategoryId=?NotificationCategoryId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?NotificationCategoryId", notificationCategoryId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<NotificationCategoryEntity> GetEntity(long notificationCategoryId)
        {
            string sql = string.Format(@"{0} WHERE NotificationCategoryId=?NotificationCategoryId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?NotificationCategoryId", notificationCategoryId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<NotificationCategoryEntity>(sql, parameters);
        }

        public async Task<IEnumerable<NotificationCategoryEntity>> GetList()
        {
            return await this.databaseContext.Slave.QueryAsync<NotificationCategoryEntity>(QUERY);
        }

        public async Task<(IEnumerable<NotificationCategoryEntity>, int)> Query(string notificationCategoryName, DateTime? start, DateTime? end, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (!string.IsNullOrWhiteSpace(notificationCategoryName))
            {
                wheres.Add("NotificationCategoryName LIKE CONCAT('%',CONCAT(?NotificationCategoryName,'%'))");
                parameters.Add("?NotificationCategoryName", notificationCategoryName, DbType.AnsiString, size: 30);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY CreateTime DESC LIMIT {2},{3}; SELECT COUNT(1) FROM OA_NotificationCategory {1}",
                   QUERY,
                   wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                   (pageIndex - 1) * pageSize,
                   pageSize);
            return await this.databaseContext.Slave.QueryMultipleAsync<NotificationCategoryEntity,int>(sql, parameters);
        }
    }
}
